package cn.sat.miniodemo.test;

import io.minio.*;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

@Slf4j
public class MinioEncryptionExample {
    public static void main(String[] args) throws Exception {
        // 1. 创建客户端
        MinioClient client = MinioClient.builder()
                .endpoint("http://localhost:9090")
                .credentials("root", "123456789")
                .build();

        String bucketName = "test-bucket";
        String objectName = "D:\\Users\\s-keyy\\Desktop\\启动\\1.txt";

        // 2. 创建桶（如果不存在）
        boolean exists = client.bucketExists(
                BucketExistsArgs.builder().bucket(bucketName).build());
        if (!exists) {
            client.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            System.out.println("✅ 桶已创建：" + bucketName);
        }

        // 3. 上传加密文件
        String content = "This is secret content";
        byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);
        ByteArrayInputStream basis = new ByteArrayInputStream(contentBytes);

        // 使用SSE-S3加密上传
        ServerSideEncryption sse = new ServerSideEncryptionS3();

        client.putObject(
                PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .stream(basis, contentBytes.length, -1)
                        .sse(sse)
                        .build()
        );

        log.info("✅ 加密文件上传成功：{}", objectName);
    }
}
